/* 
    @file temp_sensor.h
    @brief 
        
    @author ZhanZhao
    @version 1.0.0
    @date 2025/08/13
*/

#ifndef __TEMP_SENSOR_H__
#define __TEMP_SENSOR_H__

#define CONFIG_USE_TEMP_SENSOR  1
#if (CONFIG_USE_TEMP_SENSOR || 1)

#define TEMP_SENSRO_NTC       0
#define TEMP_SENSRO_PTC       1
#define TEMP_SENSOR_TYPE      TEMP_SENSRO_NTC

#define NTC_CC_VAL            100
#define _ROUND5(val)          (((val) * 2 + 1) / 2)
#define NTC_RES_TO_VOLT(res)  ((uint16_t)(NTC_CC_VAL * (res + 1000ul) / 1000ul))

#define TEMP_VAL_56           NTC_RES_TO_VOLT(3429)
#define TEMP_VAL_55           NTC_RES_TO_VOLT(3539)
#define TEMP_VAL_45           NTC_RES_TO_VOLT(4911)
#define TEMP_VAL_44           NTC_RES_TO_VOLT(5080)
#define TEMP_VAL_13           NTC_RES_TO_VOLT(15946)
#define TEMP_VAL_11           NTC_RES_TO_VOLT(17290)
#define TEMP_VAL_10           NTC_RES_TO_VOLT(18010)
#define TEMP_VAL_1            NTC_RES_TO_VOLT(26283)
#define TEMP_VAL_0            NTC_RES_TO_VOLT(27445)

#define TEMP_SENSOR_GET_TEMP()    adc_get_value_mv(6)

#if (TEMP_SENSOR_TYPE == TEMP_SENSRO_NTC)
    #define TEMP_GT(a, b)     ((a) < (b))
    #define TEMP_GE(a, b)     ((a) <= (b))
    #define TEMP_LT(a, b)     ((a) > (b))
    #define TEMP_LE(a, b)     ((a) >= (b))
    #define TEMP_EQ(a, b)     ((a) = (b))
#elif
    #define TEMP_GT(a, b)     ((a) > (b))
    #define TEMP_GE(a, b)     ((a) >= (b))
    #define TEMP_LT(a, b)     ((a) < (b))
    #define TEMP_LE(a, b)     ((a) <= (b))
    #define TEMP_EQ(a, b)     ((a) = (b))
#else
    #error "Invalid value for TEMP_SENSOR_TYPE! It should be either TEMP_SENSRO_NTC or TEMP_SENSRO_PTC"
#endif

typedef enum {
    TEMP_RANGE_UNDEFINE,
    TEMP_RANGE_LOW_CRITICAL,
    TEMP_RANGE_LOW_WARNING,
    TEMP_RANGE_NORMAL,
    TEMP_RANGE_HIGH_WARNING,
    TEMP_RANGE_HIGH_CRITICAL,
    TEMP_RANGE_NUM
} temp_rang_t;

#endif

#if (CONFIG_USE_TEMP_SENSOR)
    #include "types.h"

    void temp_sensor_init(void);
    void temp_sensor_handle(void);
    temp_rang_t temp_sensor_get_range(void);
    uint8_t temp_is_abnormal(void);
#else
    #define temp_sensor_init()
    #define temp_sensor_handle()
    #define temp_sensor_get_range()    0
    #define temp_is_abnormal()   0
#endif

#endif
